<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数字动画计数器</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fa;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: #ffffff;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            border-radius: 8px;
            text-align: center;
            width: 400px;
        }

        h1 {
            color: #4CAF50;
            margin-bottom: 20px;
        }

        p {
            font-size: 20px;
            font-weight: bold;
            color: #333;
            margin: 20px 0;
        }

        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }

        .count-display {
            font-size: 36px;
            font-weight: bold;
            color: #ff5722;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>数字动画计数器</h1>

        <p>点击按钮开始计数动画！</p>

        <div class="count-display" id="counter">0</div>

        <button onclick="startCounter()">开始计数</button>
    </div>

    <script>
        const counter = (selector, start, end, step = 1, duration = 2000) => {
            const $ = v => document.querySelector(v);
            let current = start,
                _step = (end - start) * step < 0 ? -step : step,
                timer = setInterval(() => {
                    current += _step;
                    $(selector).innerHTML = Math.floor(current);
                    if (current >= end) {
                        $(selector).innerHTML = end;
                        clearInterval(timer);
                    }
                }, Math.abs(Math.floor(duration / (end - start))));
            return timer;
        };

        function startCounter() {
            const start = 0;
            const end = 100;
            const duration = 30; // 动画持续时间
            counter("#counter", start, end, 0.05, duration); // step为0.05可以平滑过渡
        }
    </script>

</body>

</html>